home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / Forecastfox / Bin / forecastfox-0.8.5.1-fx+mz+ns.xpi / components / ffIconPack.js < prev    next >
Encoding:
Text File  |  2006-02-18  |  11.2 KB  |  351 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Forecastfox.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Jon Stritar <jstritar@MIT.EDU>.
  18.  * Portions created by the Initial Developer are Copyright (C) 2005
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  * Jon Stritar <jstritar@MIT.EDU>
  23.  * Richard Klein <richwklein@mchsi.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const IPACK_CLASS_ID = Components.ID("{318412d0-0a0e-11da-8cd6-0800200c9a66}");
  40. const IPACK_CLASS_NAME = "Forecastfox Icon Pack Component";
  41. const IPACK_CONTRACT_ID = "@ensolis.com/forecastfox/icon-pack;1";
  42. const IINFO_CLASS_ID = Components.ID("{54b44420-11af-11da-8cd6-0800200c9a66}");
  43. const IINFO_CLASS_NAME = "Forecastfox Icon Information Component";
  44. const IINFO_CONTRACT_ID = "@ensolis.com/forecastfox/icon-info;1";
  45. const ffIDisk = Components.interfaces.ffIDisk;
  46.  
  47. function ffIconPack() {};
  48.  
  49. ffIconPack.prototype = {
  50.   _id: "default",
  51.   _jar: "default.jar",
  52.   _name: "AccuWeather.com® Classic Icons",
  53.   _website: "http://www.accuweather.com/",
  54.   _author: "AccuWeather.com®",
  55.   _version: "1.0",
  56.   _smallHeight: 20,
  57.   _smallWidth: 32,
  58.   _largeHeight: 40,
  59.   _largeWidth: 64,
  60.   _target: "1.0",
  61.   _manager: null,
  62.   _exemptions: null,
  63.   
  64.   get id() { return this._id; },
  65.   get jar() { return this._jar; },
  66.   get name() { return this._name; },
  67.   get website() { return this._website; },
  68.   get author() { return this._author; },
  69.   get version() { return this._version; },
  70.   get target() { return this._target },
  71.   
  72.   init: function( aId, aJar, aName, aWebsite, aAuthor, aVersion, aTarget, aSmallHeight, 
  73.                    aSmallWidth, aLargeHeight, aLargeWidth, aCount, aExemptions)
  74.   {
  75.     this._manager = Components.classes["@ensolis.com/forecastfox/manager;1"].getService(Components.interfaces.ffIManager);
  76.     this._id = aId;
  77.     this._jar = aJar;
  78.     this._name = aName;
  79.     this._website = aWebsite;
  80.     this._author = aAuthor;
  81.     this._version = aVersion;
  82.     this._smallHeight = aSmallHeight;
  83.     this._smallWidth = aSmallWidth;
  84.     this._largeHeight = aLargeHeight;
  85.     this._largeWidth = aLargeWidth;
  86.     this._target = aTarget;
  87.     
  88.     // prepare the exemptions
  89.     this._exemptions = {};
  90.     this._exemptions['large'] = {};
  91.     this._exemptions['small'] = {};
  92.     
  93.     // process the exemptions (we need to create new ones so they have valid URIs)
  94.     for (var x = 0; x < aCount; x++) {
  95.       var exemption = aExemptions[x];
  96.       var iconInfo = Components.classes["@ensolis.com/forecastfox/icon-info;1"].createInstance(Components.interfaces.ffIIconInfo);
  97.       iconInfo.init(this._getURIOf(exemption.index, exemption.large), exemption.width, exemption.height, exemption.large, exemption.index);
  98.       this._exemptions[this._getSize(exemption.large)][exemption.index] = iconInfo;
  99.     };
  100.   },
  101.   
  102.   destroy: function()
  103.   {
  104.     this._manager = null;
  105.     this._id = null;
  106.     this._jar = null;
  107.     this._name = null;
  108.     this._website = null;
  109.     this._author = null;
  110.     this._version = null;
  111.     this._target = null;
  112.     this._smallHeight = null;
  113.     this._smallWidth = null;
  114.     this._largeHeight = null;
  115.     this._largeWidth = null;
  116.     this._exemptions = null;
  117.   },
  118.   
  119.   getIcon: function( aIndex, aLarge )
  120.   {
  121.     var iconInfo = this._exemptions[this._getSize(aLarge)][aIndex];
  122.     
  123.     // return the exemption info if it is an exemption...
  124.     if (iconInfo) return iconInfo;
  125.     
  126.     // otherwise create an icon to return
  127.     iconInfo = Components.classes["@ensolis.com/forecastfox/icon-info;1"].createInstance(Components.interfaces.ffIIconInfo);
  128.  
  129.     var width, height;
  130.     if (aLarge) {
  131.       width = this._largeWidth;
  132.       height = this._largeHeight;
  133.     } else {
  134.       width = this._smallWidth;
  135.       height = this._smallHeight;
  136.     };
  137.     
  138.     iconInfo.init(this._getURIOf(aIndex, aLarge), width, height, aLarge, aIndex);
  139.     
  140.     return iconInfo;
  141.   },
  142.   
  143.   getPreview: function()
  144.   {
  145.     //open file in zip reader
  146.     var file = this._manager.disk.get(this._jar, ffIDisk.TYPE_ICONS);
  147.     var reader = Components.classes["@mozilla.org/libjar/zip-reader;1"].createInstance(Components.interfaces.nsIZipReader);
  148.     reader.init(file);
  149.     reader.open();
  150.     
  151.     //try to get the preview entry
  152.     try {
  153.       var entry = reader.getEntry("preview.png");
  154.       reader.close();      
  155.     } catch(e) {
  156.       return "";
  157.     };
  158.     
  159.     //return url to preview  
  160.     file = this._manager.disk.get(this._jar +"!", ffIDisk.TYPE_ICONS);
  161.     file.append("preview.png");
  162.     return "jar:" + this._manager.disk.getFileURI(file); 
  163.   },
  164.   
  165.   _getURIOf: function( aIndex, aLarge )
  166.   {
  167.     // get the nsIFile of the image
  168.     var file = this._manager.disk.get(this._jar+"!", ffIDisk.TYPE_ICONS);
  169.     file.append(aLarge?"large":"small");
  170.     file.append(aIndex + ".png");
  171.     
  172.     // then convert it to a url
  173.     return "jar:" + this._manager.disk.getFileURI(file);
  174.   },
  175.  
  176.   _getSize: function( aLarge )
  177.   {
  178.     return aLarge ? "large" : "small";
  179.   },
  180.  
  181.   ///////////////////////////
  182.   // nsIClassInfo  
  183.   getInterfaces: function(aCount)
  184.   {
  185.     var ifaces = new Array();
  186.     ifaces.push(Components.interfaces.ffIIconPack);
  187.     ifaces.push(Components.interfaces.nsIClassInfo);
  188.     ifaces.push(Components.interfaces.nsISupports);
  189.     aCount.value = ifaces.length;
  190.     return ifaces;
  191.   },
  192.   
  193.   getHelperForLanguage: function(aLanguage) { return null; },
  194.   get contractID() { return IPACK_CONTRACT_ID; },
  195.   get classID() { return IPACK_CLASS_ID; },
  196.   get classDescription() { return IPACK_CLASS_NAME; },
  197.   get implementationLanguage() { return Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT; },
  198.   get flags() { return Components.interfaces.nsIClassInfo.THREADSAFE; },
  199.   
  200.   ///////////////////////////
  201.   // nsISupports
  202.   QueryInterface: function (aIID)
  203.   {
  204.     if (!aIID.equals(Components.interfaces.ffIIconPack) &&
  205.         !aIID.equals(Components.interfaces.nsIClassInfo) &&      
  206.         !aIID.equals(Components.interfaces.nsISupports))
  207.       throw Components.results.NS_ERROR_NO_INTERFACE;
  208.     return this;
  209.   }
  210. };
  211.  
  212. function ffIconInfo() {};
  213.  
  214. ffIconInfo.prototype = {
  215.   _uri: null,
  216.   _width: null,
  217.   _height: null,
  218.   _large: null,
  219.   _index: null,
  220.   
  221.   get uri() { return this._uri; },
  222.   get width() { return this._width; },
  223.   get height() { return this._height; },
  224.   get large() { return this._large; },
  225.   get index() { return this._index; },
  226.   
  227.   init: function( aUri, aWidth, aHeight, aLarge, aIndex )
  228.   {
  229.     this._uri = aUri;
  230.     this._width = aWidth;
  231.     this._height = aHeight;
  232.     this._large = aLarge;
  233.     this._index = aIndex;
  234.   },
  235.   
  236.   destroy: function()
  237.   {
  238.     this._uri = null;
  239.     this._width = null;
  240.     this._height = null;
  241.     this._large = null;
  242.     this._index = null;
  243.   },
  244.   
  245.   ///////////////////////////
  246.   // nsIClassInfo  
  247.   getInterfaces: function(aCount)
  248.   {
  249.     var ifaces = new Array();
  250.     ifaces.push(Components.interfaces.ffIIconInfo);
  251.     ifaces.push(Components.interfaces.nsIClassInfo);
  252.     ifaces.push(Components.interfaces.nsISupports);
  253.     aCount.value = ifaces.length;
  254.     return ifaces;
  255.   },
  256.   
  257.   getHelperForLanguage: function(aLanguage) { return null; },
  258.   get contractID() { return IINFO_CONTRACT_ID; },
  259.   get classID() { return IINFO_CLASS_ID; },
  260.   get classDescription() { return IINFO_CLASS_NAME; },
  261.   get implementationLanguage() { return Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT; },
  262.   get flags() { return Components.interfaces.nsIClassInfo.THREADSAFE; },
  263.   
  264.   ///////////////////////////
  265.   // nsISupports
  266.   QueryInterface: function (aIID)
  267.   {
  268.     if (!aIID.equals(Components.interfaces.ffIIconInfo) &&
  269.         !aIID.equals(Components.interfaces.nsIClassInfo) &&      
  270.         !aIID.equals(Components.interfaces.nsISupports))
  271.       throw Components.results.NS_ERROR_NO_INTERFACE;
  272.     return this;
  273.   }
  274. };
  275.  
  276. /******************************************************************************
  277.  * XPCOM Functions for construction and registration
  278.  ******************************************************************************/
  279. var gModule = {
  280.   _firstTime: true,
  281.   
  282.   _objects: {
  283.     iconPack: {
  284.       CID: IPACK_CLASS_ID,
  285.       contractID: IPACK_CONTRACT_ID,
  286.       className: IPACK_CLASS_NAME,
  287.       factory: {
  288.         createInstance: function (aOuter, aIID)
  289.         {
  290.           if (aOuter != null)
  291.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  292.           return (new ffIconPack()).QueryInterface(aIID);
  293.         }
  294.       }
  295.     },
  296.     
  297.     iconInfo: {
  298.       CID: IINFO_CLASS_ID,
  299.       contractID: IINFO_CONTRACT_ID,
  300.       className: IINFO_CLASS_NAME,
  301.       factory: {
  302.         createInstance: function (aOuter, aIID)
  303.         {
  304.           if (aOuter != null)
  305.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  306.           return (new ffIconInfo()).QueryInterface(aIID);
  307.         }
  308.       }
  309.     }
  310.   },
  311.   
  312.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  313.   {
  314.     if (this._firstTime) {
  315.       this._firstTime = false;
  316.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  317.     };
  318.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  319.     for (var key in this._objects) {
  320.       var obj = this._objects[key];
  321.       aCompMgr.registerFactoryLocation(obj.CID, obj.className, obj.contractID,
  322.                                        aFileSpec, aLocation, aType);
  323.     };
  324.   },
  325.  
  326.   unregisterSelf: function(aCompMgr, aLocation, aType)
  327.   {
  328.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  329.     for (var key in this._objects) {
  330.       var obj = this._objects[key];
  331.       aCompMgr.unregisterFactoryLocation(obj.CID, aLocation);
  332.     };      
  333.   },
  334.   
  335.   getClassObject: function(aCompMgr, aCID, aIID)
  336.   {
  337.     if (!aIID.equals(Components.interfaces.nsIFactory))
  338.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  339.  
  340.     for (var key in this._objects) {
  341.       if (aCID.equals(this._objects[key].CID))
  342.         return this._objects[key].factory;
  343.     };
  344.  
  345.     throw Components.results.NS_ERROR_NO_INTERFACE;
  346.   },
  347.  
  348.   canUnload: function(aCompMgr) { return true; }
  349. };
  350.  
  351. function NSGetModule(aCompMgr, aFileSpec) { return gModule; }